package com.liqi.frame.comm.service.impl;

import com.liqi.frame.comm.dao.GroupDao;
import com.liqi.frame.comm.dao.RoleDao;
import com.liqi.frame.comm.dao.RolePermissionDao;
import com.liqi.frame.comm.dao.UserRoleDao;
import com.liqi.frame.comm.enums.PermissionTypeEnum;
import com.liqi.frame.comm.po.Group;
import com.liqi.frame.comm.po.Role;
import com.liqi.frame.comm.po.RolePermission;
import com.liqi.frame.comm.po.User;
import com.liqi.frame.comm.service.PermissionService;
import com.liqi.frame.comm.service.RoleService;
import com.liqi.frame.comm.token.TokenUtil;
import com.liqi.frame.comm.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * Created by liqi on 2017/8/2.
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Override
    public Set<String> findRoleByUserId(String userId) {
        return roleDao.findRoleByUserId(userId);
    }

    @Override
    public List<Role> selectList(String roleKey) {
        if (roleKey!=null) {
            roleKey = roleKey.trim();
        }
        User currentUser = TokenUtil.getToken();
        List<Group> groupList = groupDao.getGroupAndSonsByGroupId(currentUser.getGroupId());
        if (groupList!=null && groupList.size()>0) {
            String[] groupIds = new String[groupList.size()];
            for (int i = 0; i < groupList.size(); i++) {
                groupIds[i] = groupList.get(i).getId();
            }
            return roleDao.selectList(roleKey, groupIds);
        }
        return null;
    }

    @Override
    public Role getRoleById(String id) {
        return roleDao.getObjectById(id);
    }

    @Override
    public List<Role> getRoleListByKey(String roleKey) {
        return roleDao.getRoleListByKey(roleKey);
    }

    @Override
    public List<Role> getRoleListWithName(String roleName, String[] groupIds) {
        if (roleName!=null && !Objects.equals(roleName, "null")) {
            roleName = roleName.trim();
        }
        if (groupIds!=null && !Objects.equals(groupIds, "groupIds") && groupIds.length>0) {
            return roleDao.getRoleListWithName(roleName, groupIds);
        }
        return null;
    }

    @Override
    public List<Role> getRoleListWithNameByUserId(String roleName, String[] groupIds, String userId) {
        if (roleName!=null) {
            roleName = roleName.trim();
        }
        if (groupIds!=null && groupIds.length>0) {
            return roleDao.getRoleListWithNameByUserId(roleName, groupIds, userId);
        }
        return null;
    }

    @Override
    public int insertRole(String id, String roleKey, String roleName, String comment, Float orderId, String groupId, String groupName) {
        if (groupId!=null && !Objects.equals("",groupId)) {
            return roleDao.insertObject(id, roleKey, roleName, comment, orderId, groupId, groupName);
        } else {
            User currentUser = TokenUtil.getToken();
            if (currentUser != null && currentUser.getGroupId() != null && !Objects.equals("", currentUser.getGroupId())) {
                return roleDao.insertObject(id, roleKey, roleName, comment, orderId, currentUser.getGroupId(), currentUser.getGroupName());
            }
        }
        return 0;
    }

    @Override
    public int updateRole(String id, String roleKey, String roleName, String comment, Float orderId, String groupId, String groupName) {
        if (groupId!=null && !Objects.equals("",groupId)) {
            return roleDao.updateObject(id, roleKey, roleName, comment, orderId, groupId, groupName);
        } else {
            User currentUser = TokenUtil.getToken();
            if (currentUser != null && currentUser.getGroupId() != null && !Objects.equals("", currentUser.getGroupId())) {
                return roleDao.updateObject(id, roleKey, roleName, comment, orderId, currentUser.getGroupId(), currentUser.getGroupName());
            }
        }
        return 0;
    }

    @Override
    public int updateOrderId(String id, Float orderId) {
        return roleDao.updateObject(id, null, null, null, orderId, null, null);
    }

    @Override
    public int deleteRoleById(String id) {
        int resultCount = 0;
        resultCount = roleDao.deleteObjectById(id);
        rolePermissionDao.deleteRoleAndPermissionsByRoleId(id);
        userRoleDao.deleteUserAndRolesByRoleId(id);
        return resultCount;
    }

    @Override
    public List<Role> getRoleListByUsername(String username) {
        return roleDao.getRolesByUsername(username);
    }

    @Override
    public List<Role> getRoleListByUserId(String userId) {
        return roleDao.getRolesByUserId(userId);
    }

    @Override
    public int saveRoleAndMenus(String roleId, String[] menuIds) {
        int resultCount = 0;
        List<RolePermission> rolePermissionList = rolePermissionDao.getRoleAndPermissionsByRoleId(roleId, PermissionTypeEnum.MENU.getKey());
        if (rolePermissionList.size()>0) {
            String[] rolePermissionIdsForDel = new String[rolePermissionList.size()];

            for (int i = 0; i < rolePermissionList.size(); i++) {
                rolePermissionIdsForDel[i] = rolePermissionList.get(i).getId();
            }
            // 删除原有角色和菜单的关联数据
            rolePermissionDao.deleteRoleAndPermissionsByIds(rolePermissionIdsForDel);
        }

        if (menuIds==null) {
            return resultCount;
        }
        // 添加新的角色和菜单的关联数据
        List<RolePermission> rolePermissionsForAdd = new ArrayList<>();
        for (int j = 0; j < menuIds.length; j++) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setId(IdUtil.getId()+"");
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(menuIds[j]);
            rolePermissionsForAdd.add(rolePermission);
        }
        if (rolePermissionsForAdd.size()>0) {
            resultCount = rolePermissionDao.insertRoleAndPermissions(rolePermissionsForAdd);
        }

        return resultCount;
    }

    @Override
    public int saveRoleAndOperations(String roleId, String[] OperationIdsIds) {
        int resultCount = 0;
        List<RolePermission> rolePermissionList = rolePermissionDao.getRoleAndPermissionsByRoleId(roleId, PermissionTypeEnum.OPERATION.getKey());
        if (rolePermissionList.size()>0) {
            String[] rolePermissionIdsForDel = new String[rolePermissionList.size()];

            for (int i = 0; i < rolePermissionList.size(); i++) {
                rolePermissionIdsForDel[i] = rolePermissionList.get(i).getId();
            }
            // 删除原有角色和操作的关联数据
            rolePermissionDao.deleteRoleAndPermissionsByIds(rolePermissionIdsForDel);
        }

        if (OperationIdsIds==null) {
            return resultCount;
        }
        // 添加新的角色和操作的关联数据
        List<RolePermission> rolePermissionsForAdd = new ArrayList<>();
        for (int j = 0; j < OperationIdsIds.length; j++) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setId(IdUtil.getId()+"");
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(OperationIdsIds[j]);
            rolePermissionsForAdd.add(rolePermission);
        }
        if (rolePermissionsForAdd.size()>0) {
            resultCount = rolePermissionDao.insertRoleAndPermissions(rolePermissionsForAdd);
        }
        return resultCount;
    }

    @Override
    public List<Role> getRoleListByGroupId(String groupId) {
        return roleDao.getRoleListByGroupId(groupId);
    }

    @Resource
    private RoleDao roleDao;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private GroupDao groupDao;
}
